package com.traning;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class testServlet
 */
public class testServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public testServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("From Init Method");
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("From destroy method");
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		PrintWriter writer = response.getWriter();
		response.setContentType("text/html");
		String FromOtherServlet = (String) request.getServletContext().getAttribute("DB_UserName");
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String dob = request.getParameter("dob");
		String address = request.getParameter("address");
		String phone = request.getParameter("phone");
		String[] tech = request.getParameterValues("tech");
		String gender = request.getParameter("gender");
		
		if(name == null){
			response.sendRedirect("register.html");
		}
		else{
	        
			HttpSession session = request.getSession();
			session.setAttribute("name", name);
			response.sendRedirect("Welcome.jsp");
		}
		response.getWriter().write("From Get Method--<br/> Welcome to "+ name +"<br/> Password is "+ password +"<br/> Date Of Birth is : "+dob+"<br/> Address is : "+ address +"<br/> PhoneNumber is : "+ phone +"<br/>Information From Other Servlet is---"+FromOtherServlet);
		response.getWriter().write("<br/>Gender is : "+gender);
		if(FromOtherServlet!=null){
			response.getWriter().write(" <br/>From other servlet  ---"+FromOtherServlet);	
		}
		else{
			response.getWriter().println("<br/>No information from other servlet  ---<br/>");
		}
		response.getWriter().println("<br/>You Selected <br/>");
		if(tech != null)
		{
		for (int i = 0; i < tech.length; i++) {
		
			writer.print(tech[i]+"<br/>");
		}
		}
		else
		{
			response.getWriter().println("--No items selected--");
		}
         
		writer.close();
		
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
		// TODO Auto-generated method stub
	}
}
	